package smartq.app;

import java.sql.ResultSet;

import smartq.DB.Connect;
import smartq.DB.Customers;

import com.app.smart_q.R;

import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;

public class SmartQ_Tap_Main extends TabActivity {
	
	private Handler mHandler = new Handler();
	int i=0;
	
	Connect con = new Connect(); 
	ResultSet rs;
	String   OID
			,CustomerFirstName
			,CustomerLastName
			,CustomerSex
			,CustomerClass
			,CustomerPhonenumber;
	
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.smartq_tapmenu);
		
		Bundle bundle = getIntent().getExtras();
		CustomerPhonenumber = ((String) bundle.get("CustomerPhonenumber"));
		
		DefineCustomer(CustomerPhonenumber);
		
		
		final AlertDialog.Builder dDialog = new AlertDialog.Builder(this);
		
		if(CustomerFirstName.equals("")){
			dDialog.setTitle("ยินดีต้อนรับสู่โปรแกรม SmartQ");
			dDialog.setIcon(android.R.drawable.btn_star_big_on);
			dDialog.setMessage(CustomerPhonenumber+"\nคุณเข้าใช้งานครั้งเเรก\nกรุณาเพิ่มข้อมูลในหน้า Info ด้วยค่ะ");
			dDialog.setPositiveButton("ยืนยัน", null);
			dDialog.show();
		}else{
//			dDialog.setTitle("ยินดีต้อนรับสู่โปรแกรม SmartQ");
//			dDialog.setIcon(android.R.drawable.btn_star_big_on);
//			dDialog.setMessage(CustomerPhonenumber+"\nคุณ "+CustomerFirstName+" "+CustomerLastName);
//			dDialog.setPositiveButton("ยืนยัน", null);
//			dDialog.show();
		}
		
		mHandler.removeCallbacks(mCheckDB);
		mHandler.postDelayed(mCheckDB, 500);
		
		//---แจ้งเตือน-------
//		final NotificationManager notiManager = (NotificationManager) 
//        		getSystemService(Context.NOTIFICATION_SERVICE);
//        
//        final Notification noti = new Notification(R.drawable.ic_launcher, 
//        		"Notification Text", System.currentTimeMillis());
//        Intent intent = new Intent(this, SmartQ_splashActivity.class);
//        PendingIntent destIntent = PendingIntent.getActivity(this, 0, intent, 0);
//
//        noti.setLatestEventInfo(this, "SmartQ", "Notification Content", destIntent);
//        
//		noti.defaults |= Notification.DEFAULT_SOUND;
//		noti.defaults |= Notification.DEFAULT_VIBRATE;
//		notiManager.notify(3, noti);
		
		//-------------------------------------------------
		
		TabHost tabHost = getTabHost();

		TabSpec bookspec = tabHost.newTabSpec("book");
		bookspec.setIndicator("Booking");
		Intent bookIntent = new Intent(this, SmartQ_Main_Activity.class);
		bookIntent.putExtra("CustomerPhonenumber", CustomerPhonenumber);
		bookspec.setContent(bookIntent);

		TabSpec queuespec = tabHost.newTabSpec("queue");
		queuespec.setIndicator("Queue");
		Intent queueIntent = new Intent(this, SmartQ_Manage_Activity.class);
		queueIntent.putExtra("CustomerPhonenumber", CustomerPhonenumber);
		queuespec.setContent(queueIntent);


		TabSpec infospec = tabHost.newTabSpec("info");
		infospec.setIndicator("info");
		Intent infoIntent = new Intent(this, SmartQ_info_Activity.class);
		infoIntent.putExtra("CustomerPhonenumber", CustomerPhonenumber);
		infoIntent.putExtra("CustomerFirstName", CustomerFirstName);
		infoIntent.putExtra("CustomerLastName", CustomerLastName);
		infoIntent.putExtra("Customersex", CustomerSex);
		infoIntent.putExtra("CustomerClass", CustomerClass);
		infospec.setContent(infoIntent);

		tabHost.addTab(bookspec);
		tabHost.addTab(queuespec);
		tabHost.addTab(infospec);
	}
	
	private Runnable mCheckDB = new Runnable(){
		
		public void run() {		
			i++;
		} 	
    }; 
    
    private void DefineCustomer(String CustomerPhone){
    	rs = null;
    	Customers usr = new Customers();
		try {
			rs = usr.selectCustomers("", "",CustomerPhone);
			
			if (rs.next()) {
				OID = rs.getString("OID");
				CustomerPhonenumber = rs.getString("CustomerPhonenumber");
				CustomerFirstName = rs.getString("CustomerFirstName");
				CustomerLastName = rs.getString("CustomerLastName");
				CustomerSex = rs.getString("Customersex");
				CustomerClass = rs.getString("CustomerClass");
				
			}else{
				CustomerPhonenumber = CustomerPhone;
				CustomerFirstName = ("");
				CustomerLastName = ("");
				CustomerSex = ("");
				CustomerClass = ("");
			}
		}catch (Exception e) {
			
		}
    }
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
//		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}


	
	 @Override
		public void onBackPressed() {
		  
		        new AlertDialog.Builder(this)
		        .setTitle("ออกจากโปรแกรม")
		        .setMessage("ต้องการออกจากโปรแกรม")
		                .setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {

		                    public void onClick(DialogInterface arg0, int arg1) {
		                        finish();
		                        System.exit(0);
		                    }
		                })
		                .setPositiveButton(android.R.string.no, null).create().show();
		    

		} 
}
